package gapidt.plugin.actions;

import gapidt.plugin.wizards.WsdlGeneratorWizard;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorPart;

public class GenerateWsdlFromEditor implements IEditorActionDelegate {
	
	IEditorPart editor = null;

	@Override
	public void run(IAction arg0) {
		if(editor != null){
			
			ICompilationUnit iCompilationUnit = JavaUI.getWorkingCopyManager().getWorkingCopy(editor.getEditorInput());
			
			try {
				if(iCompilationUnit != null && iCompilationUnit.hasChildren()){
					IJavaProject javaProject = JavaCore.create(iCompilationUnit.getResource().getProject());
					WsdlGeneratorWizard wizard = new WsdlGeneratorWizard(iCompilationUnit.getAllTypes(), javaProject);
					WizardDialog dialog = new WizardDialog(editor.getSite().getShell(), wizard);
					dialog.create();
					dialog.open();
				}
			} catch (JavaModelException e) {
				e.printStackTrace();
			}
		}
	}
	
	@Override
	public void selectionChanged(IAction arg0, ISelection arg1) {
		
	}
	
	@Override
	public void setActiveEditor(IAction action, IEditorPart currentEditor) {
		if(currentEditor != null)
			editor = currentEditor;
	}
	
}
